﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using SlamCms.Common;

namespace SlamCms.SharePoint.Layouts.SlamCMS
{
    public partial class SyncSites : LayoutsPageBase
    {

        protected override void OnInit(EventArgs e)
        {
            btnSync.Click += new EventHandler(btnSync_Click);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        void btnSync_Click(object sender, EventArgs e) 
        {

            var source = SPContext.Current.Site.OpenWeb("/Content");
            Guid publishingSiteStructure = new Guid("5bf84649-8c21-431b-808c-ad22435a72b0");
            var publishingSites = SPContext.Current.Site.AllWebs.Where(o => o.Features.Any(f => f.DefinitionId.Equals(publishingSiteStructure))).ToList();

            List<SyncTrace> process = new List<SyncTrace>();

            foreach (SPWeb destination in publishingSites)
            {
                process.AddRange(PublishingSiteService.SyncSite(source,destination));
            }

            txtResult.Text = FormatTrace(process);
            //this.ltResult.Text = FormatTrace(processTrace);
        }

        private string FormatTrace(List<SyncTrace> processTrace)
        {
            StringBuilder builder = new StringBuilder();

            var result = processTrace.Where(o => o.Relevance.Equals(Relevance.ProcessMessage) && o.GroupID.Split('-').Length == 1).OrderBy(o=> o.TimeStamp).ToList();

            //string groupPrimaryID = "";

            foreach (SyncTrace t in result)
            {
                string groupIDSite = t.GroupID.Split('-')[0];

                var siteTrace = processTrace.Where(o => o.Relevance.Equals(Relevance.ProcessMessage) && o.GroupID.Contains(groupIDSite)).OrderBy(o => o.TimeStamp).ToList();
                builder.AppendLine(FormatTraceSite(siteTrace));
                builder.AppendLine("===============================================================================================================");
                //builder.AppendLine("");
               

                //builder.AppendLine(t.Message);
                //builder.Append();
            }

            return builder.ToString();
        }

        private string FormatTraceSite(List<SyncTrace> siteTrace)
        {
            StringBuilder builder = new StringBuilder();
            string groupPrimaryID = "";

            foreach (SyncTrace t in siteTrace)
            {
                var tempGroupID = t.GroupID.Split('-');
                string tempID = tempGroupID.Length > 2 ? (tempGroupID[0] + "-" + tempGroupID[1] +"-" +tempGroupID[2]) : t.GroupID;
                if (!groupPrimaryID.Equals(tempID))
                {
                    builder.AppendLine("");
                    groupPrimaryID = tempID;
                }

                builder.AppendLine(t.Message);
            }

            return builder.ToString();
        }

    }
}
